#include "camera.h"
#include <libv4l2.h>
#include <Effect/effect.h>


Camera::Camera(const int cameraIndex, int buffer_size) :
    RMVideoCapture(cameraIndex),
    index(cameraIndex)
{
}

Camera::Camera() :
    RMVideoCapture()
{
}

Camera::~Camera() {
//    videoCap.release();
    std::cout << "关闭了相机." << std::endl;
}

void Camera::setParameters(cv::Size videoSize,
                           int exposureTime,
                           int fps,
                           bool mjpg) {
    //Initialize camera
    setVideoFormat(videoSize.width, videoSize.height, mjpg);
    setExposureTime(exposureTime);
    setFPS(fps);
    this->fps = fps;
    startStream();
    info();
}

#ifdef USE_OPNECV
bool Camera::open() {
    videoCap.open(index);
    setSrcImg();
    status = videoCap.isOpened();
    return status;
}

bool Camera::open(int cameraIndex) {
    videoCap.open(cameraIndex);
    setSrcImg();
    status = videoCap.isOpened();
    return status;
}
#endif

bool Camera::close(int cameraIndex) {
//    if (status) {
//        v4l2_close(0);
//    }
//    else {
//        std::cout << "未打开相机，无需关闭." << std::endl;
//    }
    v4l2_close(cameraIndex);
    return true;
}

void Camera::start() {
    timer.start((int)(1000 / fps));
}

void Camera::pause() {
    timer.stop();
}


void Camera::showPauseImg() {
    cv::Mat lucencyImg = cv::imread("../../resource/image/icon/pauseIcon.png", -1);

    Effect::lucencyImgOverlay(lucencyImg, srcImg, cv::Point(srcImg.cols/2, srcImg.rows/2));

    std::cout << "Pause!" << std::endl;
}

void Camera::setSrcImg() {
    srcImg = cv::Mat::zeros(videoCap.get(CV_CAP_PROP_FRAME_HEIGHT),
                            videoCap.get(CV_CAP_PROP_FRAME_WIDTH), CV_8UC3);
}
